/**
 * 差旅标准信息控制类
 * @author zqd	差旅小组
 *
 * 上午09:53:19
 */
package com.cbts.action.info;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;

import com.cbts.entity.BusinessTravel;
import com.cbts.entity.Hotel;
import com.cbts.entity.Traffic;
import com.cbts.entity.TravelStandard;
import com.cbts.service.info.TravelInfoService;
import com.cbts.util.ObjectJsonValueProcessor;
import com.cbts.util.Page;
import com.opensymphony.xwork2.ActionSupport;


public class TravelInfoAction extends ActionSupport {
	
	private TravelInfoService travelInfoService;

//	private List<TravelStandard> tsList;
	private List<Hotel> htList;		//用于差旅标准数据准备
	private List<Traffic> tfList;	//用于差旅标准数据准备
	
	private String jsonResult;		//用于前台传输数据的json结果串
	private String message;			//返回前台的消息
	
	private Hotel hotel;
	private Traffic traffic;
	private TravelStandard travelStandard;

	public void setTravelInfoService(TravelInfoService travelInfoService) {
		this.travelInfoService = travelInfoService;
	}
	



	public List<Hotel> getHtList() {
		return htList;
	}
	public void setHtList(List<Hotel> htList) {
		this.htList = htList;
	}
	public List<Traffic> getTfList() {
		return tfList;
	}
	public void setTfList(List<Traffic> tfList) {
		this.tfList = tfList;
	}
	public Hotel getHotel() {
		return hotel;
	}
	public void setHotel(Hotel hotel) {
		this.hotel = hotel;
	}
	public Traffic getTraffic() {
		return traffic;
	}
	public void setTraffic(Traffic traffic) {
		this.traffic = traffic;
	}
	public TravelStandard getTravelStandard() {
		return travelStandard;
	}
	public void setTravelStandard(TravelStandard travelStandard) {
		this.travelStandard = travelStandard;
	}

	
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 数据准备,作废
	 * @return
	 */
	public String prepareSetting(){
		try{
			//this.htList=travelInfoService.getInfo(0).getPageItems();
			//this.tfList=travelInfoService.getInfo(1).getPageItems();
			return "preparesetting";
		}catch(Exception e){
			e.printStackTrace();
			return ERROR;
		}
	}
	
	/**
	 * 显示信息列表
	 * @return
	 */
	public String showInfoList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type=request.getParameter("type");
		int page=1;//获得参数
		
		if(request.getParameter("page")!=null)
			page=Integer.parseInt(request.getParameter("page"));
		
		JSONArray jsondata=new JSONArray();							//转换工具
		JSONObject jsonobject=new JSONObject();
		StringBuffer buffer=new StringBuffer();
		
		//选择类型,0-hotel,1-traffic,2-travelStandard
		if(type.compareTo("0")==0){	//
			Page<Hotel> hotellist=travelInfoService.getInfo(0,page);		//获得所有条目		
			//
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"travelStandards"});	//过滤
			jsondata=JSONArray.fromObject(hotellist.getPageItems(),config);
			
			buffer.append("{\"hotellist\":").append(jsondata.toString());
			buffer.append(",\"pagenum\":").append(hotellist.getPageNo());
			buffer.append(",\"pages\":").append(hotellist.getTotalPageNum()).append("}");
			jsonResult=buffer.toString();							//转换为string
		}else if(type.compareTo("1")==0){
			Page<Traffic> trafficlist=travelInfoService.getInfo(1,page);
			
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"travelStandards"});	//过滤
			
			jsondata=JSONArray.fromObject(trafficlist.getPageItems(),config);
			
			buffer.append("{\"trafficlist\":").append(jsondata.toString());
			buffer.append(",\"pagenum\":").append(trafficlist.getPageNo());
			buffer.append(",\"pages\":").append(trafficlist.getTotalPageNum()).append("}");
			jsonResult=buffer.toString();							//转换为string
		}else if(type.compareTo("2")==0){
			Page<Hotel> hotellist=travelInfoService.getInfo(0);
			Page<Traffic> trafficlist=travelInfoService.getInfo(1);
			Page<TravelStandard> tslist=travelInfoService.getInfo(2,page);
			
			JsonConfig tsconfig=new JsonConfig();
			tsconfig.setExcludes(new String[]{"businessTravels","userGroups"});				//过滤
			tsconfig.registerJsonValueProcessor(Traffic.class,
					new ObjectJsonValueProcessor(new String[]{"trafficId"},Traffic.class));	//保留trafficid
			tsconfig.registerJsonValueProcessor(Hotel.class,
					new ObjectJsonValueProcessor(new String[]{"hotelId"},Hotel.class));		//保留hotelId
			
			JsonConfig hoconfig=new JsonConfig();								//hotel
			hoconfig.setExcludes(new String[]{"travelStandards","price"});
			JsonConfig trconfig=new JsonConfig();								//traffic
			trconfig.setExcludes(new String[]{"travelStandards","price"});
			jsondata=JSONArray.fromObject(hotellist.getPageItems(),hoconfig);
			buffer.append("{\"hotellist\":").append(jsondata.toString());		//转换为string
			jsondata=JSONArray.fromObject(trafficlist.getPageItems(), trconfig);
			buffer.append(",\"trafficlist\":").append(jsondata.toString());
			jsondata=JSONArray.fromObject(tslist.getPageItems(),tsconfig);
			buffer.append(",\"tslist\":").append(jsondata.toString());
			buffer.append(",\"pagenum\":").append(tslist.getPageNo());
			buffer.append(",\"pages\":").append(tslist.getTotalPageNum()).append("}");
			jsonResult=buffer.toString();
		}
		return "showinfolist";		//返回消息，并传入jsonResult
	}
	
	/**
	 * 更新信息
	 * @return
	 */
	public String updateInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type=request.getParameter("type");				//同样是接收参数
		try{
			message="";
			String hint=null;
			
			//选择类型,0--交通方式，1--住宿，2--差旅标准
			if(type.compareTo("0")==0){	//
				hint=validation(0,1);
				if(hint.isEmpty()){			//hint为empty
					if(!travelInfoService.updateInfo(this.getHotel(), 0))
						message="fail";		//更新失败
				}else message=hint;
			}else if(type.compareTo("1")==0){
				hint=validation(1,1);
				if(hint.isEmpty()){			//hint为empty
					if(!travelInfoService.updateInfo(this.getTraffic(), 1))
						message="fail";		//更新失败	
				}else message=hint;
			}else if(type.compareTo("2")==0){
				hint=validation(2,1);
				if(hint.isEmpty()){			//hint为empty
					if(!travelInfoService.updateInfo(this.getTravelStandard(), 2))
						message="fail";		//更新失败
				}else message=hint;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "updateinfo";
	}
	
	/**
	 * 删除信息
	 * @return
	 */
	public String deleteInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type=request.getParameter("type");			//同样是接收参数
		int id=Integer.parseInt(request.getParameter("id"));
		try{
			message="";
			//选择类型,0--交通方式，1--住宿，2--差旅标准
			if(type.compareTo("0")==0){	//
				if(!travelInfoService.deleteInfo(travelInfoService.getHotelInfoById(id), 0))
					message="fail";			//删除失败
			}else if(type.compareTo("1")==0){
				if(!travelInfoService.deleteInfo(travelInfoService.getTrafficInfoById(id), 1))
					message="fail";			//删除失败
			}else if(type.compareTo("2")==0){
				if(!travelInfoService.deleteInfo(travelInfoService.getTravelStandardInfoById(id), 2))
					message="fail";			//删除失败
			}
			message="success";
			
		}catch(Exception e){
			e.printStackTrace();
			message="error";
		}
		return "deleteinfo";
	}
	
	/**
	 * 添加信息
	 * @return
	 */
	public String addInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type=request.getParameter("type");	//同样是接收参数
		try{
			message="";
			String hint=null;
			//选择类型,0--交通方式，1--住宿，2--差旅标准
			if(type.compareTo("0")==0){	//
				hint=validation(0,0);
				if(hint.isEmpty()){			//hint为empty
					if(!travelInfoService.saveInfo(this.getHotel(), 0))
						message="fail";		//添加失败
				}else message=hint;
			}else if(type.compareTo("1")==0){
				hint=validation(1,0);
				if(hint.isEmpty()){
					if(!travelInfoService.saveInfo(this.getTraffic(), 1))
						message="fail";		//添加失败
				}else message=hint;
			}else if(type.compareTo("2")==0){
				hint=validation(2,0);
				if(hint.isEmpty()){
					if(!travelInfoService.saveInfo(this.getTravelStandard(), 2))
						message="fail";		//添加失败
				}else message=hint;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "addinfo";
	}
	
	private String validation(int type,int oper){	//type--类型，oper--操作：0-add,1-update
		try{
			StringBuffer hint=new StringBuffer();
			Pattern p=Pattern.compile("^-?\\d+\\.?\\d{0,2}$");
			if(type==0){
					if(this.hotel.getHotel().equals(""))
						hint.append("酒店级别不能为空\n");
					else{
						if(oper==0){ 
							if(travelInfoService.existsCheck(new Object[]{this.hotel.getHotel()}, 0, 0))
								hint.append("该酒店级别已存在\n");
							}
						else if(oper==1){
							if(travelInfoService.existsCheck(new Object[]{this.hotel.getHotel(),this.hotel.getHotelId()}, 0, 1))
								hint.append("该酒店级别已存在\n");
						}
					}
				if(this.hotel.getPrice()==null||this.hotel.getPrice()==0)
					hint.append("酒店价格不能为空或为0\n");
				else if(this.hotel.getPrice().isNaN())
					hint.append("价格格式错误\n");
				else if(!(p.matcher(this.hotel.getPrice().toString())).find())
					hint.append("请输入范围在000000000.00~999999999.99的至多两位小数的数字\n");
			}else if(type==1){
				if(this.traffic.getTraffic().equals("")||this.traffic.getStandard().equals("")){
					if(this.traffic.getTraffic().equals(""))
						hint.append("交通方式不能为空\n");
					if(this.traffic.getStandard().equals(""))
						hint.append("交通方式级别不能为空\n");
				}else{
					if(oper==0){
						if(travelInfoService.existsCheck(new Object[]{this.traffic.getTraffic(),this.traffic.getStandard()}, 1, 0))
							hint.append("该交通方式已存在\n");
					}else if(oper==1){
						if(travelInfoService.existsCheck(new Object[]{this.traffic.getTraffic(),this.traffic.getStandard(),this.traffic.getTrafficId()}, 1, 1))
							hint.append("该交通方式已存在\n");
					}
				}
				if(this.traffic.getPrice()==null||this.traffic.getPrice()==0)
					hint.append("交通方式价格不能为空或为0\n");
				else if(this.traffic.getPrice().isNaN())
					hint.append("价格格式错误\n");
				else if(!(p.matcher(this.traffic.getPrice().toString())).find())
					hint.append("请输入范围在000000000.00~999999999.99的至多两位小数的数字\n");
			}else if(type==2){
				if(this.travelStandard.getGrade().equals(""))
					hint.append("差旅标准级别不能为空\n");
				else{
					if(oper==0){
						if(travelInfoService.existsCheck(new Object[]{this.travelStandard.getGrade()}, 2, 0))
							hint.append("该差旅标准已存在\n");
					}else if(oper==1){
						if(travelInfoService.existsCheck(new Object[]{this.travelStandard.getGrade(),this.travelStandard.getStandardId()}, 2, 1))
							hint.append("该差旅标准已存在\n");
					}
				}
				if(this.travelStandard.getMealsCost()==null||this.travelStandard.getElseCost()==null
						||this.travelStandard.getMealsCost()==0||this.travelStandard.getElseCost()==0)
					hint.append("差旅标准费用不能为空或为0\n");
				else if(this.travelStandard.getMealsCost().isNaN()||this.travelStandard.getElseCost().isNaN())
					hint.append("费用格式错误\n");
				else if((!(p.matcher(this.travelStandard.getMealsCost().toString())).find())||(!(p.matcher(this.travelStandard.getElseCost().toString())).find()))
					hint.append("请输入范围在000000000.00~999999999.99的至多两位小数的数字\n");
			}
			return hint.toString();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
